﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YunCMS.Model;
using YunCMS.IDAL;

namespace YunCMS.BLL
{
    public abstract class ModuleBLL<T> where T : YCModule, new()
    {

        protected abstract IModuleDAL<T> DAL
        {
            get;
        }


        #region Insert update delete
        /// <summary>
        /// insert a YCModuleArtical
        /// </summary>
        /// <param name="model">YCModuleArtical</param>
        /// <returns>The id of the new item</returns>
        public int Add(T model)
        {
            // Validate input
            if (model == null)
                return 0;
            // Use the dal to insert a new record 
            return DAL.Add(model);
        }

        /// <summary>
        /// update a YCModuleArtical.
        /// </summary>
        /// <param name="model">YCModuleArtical</param>
        /// <returns>Rows affected</returns>
        public int Update(T model)
        {
            // Validate input
            if (model == null)
                return 0;
            // Use the DAL to update a new record 
            return DAL.Update(model);
        }

        /// <summary>
        /// delete a item
        /// </summary>
        /// <param name="model">id</param>
        /// <returns>affectes rows</returns>
        public int Remove(int id)
        {
            // Validate input
            if (id < 0)
                return 0;
            return DAL.Delete(id);
        }

        /// <summary>
        /// Get a YCModuleArtical 
        /// </summary>
        /// <param name="ID">id</param>
        /// <returns>YCModuleArtical entity</returns>
        public T GetModule(int id)
        {
            // Validate input
            if (id < 0)
                return null;

            // Use the DAL to get a record 
            return DAL.GetModel(id);
        }

        /// <summary>
        /// get YCModuleArticals
        /// </summary>
        /// <returns>List</returns>
        public IList<T> GetList(string sqlstr)
        {
            // Use the DAL to get all records 
            return DAL.GetList(sqlstr);
        }

        public int UpdateActive(int id, bool value)
        {
            return DAL.UpdateOneField(id, "Active", value);
        }

        public int UpdateIsHome(int id, bool value)
        {
            return DAL.UpdateOneField(id, "IsHomePage", value);
        }

        public int UpdateIsPop(int id, bool value)
        {
            return DAL.UpdateOneField(id, "IsPopular", value);
        }

        public int GetCount(string title, string check, string top)
        {
            if (title == null || check == null || top == null)
            {
                throw new ArgumentException("参数不正确");
            }

            return DAL.GetCount(title, check, top);
        }



        public IList<T> GetList(string title, string check, string top, int pageSize, int pageCurrent)
        {
            if (title == null || check == null || top == null || pageSize < 1 || pageCurrent < 0)
            {
                throw new ArgumentException("参数不正确");
            }
            return DAL.GetList(title, check, top, pageSize, pageCurrent);
        }

        public IList<T> GetList(int top, string fields, string strwhere, string order)
        {
            if (strwhere == null || order == null || fields== null)
            {
                throw new ArgumentException("参数不正确");
            }
            if (fields.Equals(string.Empty))
            {
                return DAL.GetList(top, fields, strwhere, order);
            }
            return DAL.GetList(top, fields, strwhere, order);
        }

        /// <summary>
        /// if IsExist a item accroding to id
        /// </summary>
        /// <param name="ID">id</param>
        /// <returns>true/false</returns>
        public bool IsExist(int id)
        {
            return DAL.IsExist(id);
        }

        #endregion
    }
}
